<?php

namespace App\Http\Controllers\Home;

use App\Models\Addr;
use App\Models\Goods;
use Illuminate\Http\Request;



//使用类
use App\Http\Controllers\Controller;


//购物车控制器
class CarController extends Controller{
    //购物车页
    public function index(){
       // dd(session("shop"));
        //把session中的商品获取到
        $shop= session("shop");
       // dd($shop);
       // foreach($shop as &$v){
        //    $v->money=$v['num']*$v['goodsInfo']->price;
        //}




        return view("home.car")->with("shop",$shop);
    }

    //加入购物车方法
    public function addCar(Request $request){
/*
        //数据处理
        $data = array();
        $arr[]=array(
            "id"=>$request->input('id'),//$_GET['id']
            "num"=>$request->input('num'),
        );
        // 通过 HTTP 请求实例...
        $request->session()->put('shop', $arr);*/

        $data = session('shop')?session('shop'):array();

        $a=0;
        if($data){
            foreach($data as $key => &$value){//&按地址引用
                if($value['id']==$request->input('id')){//id session中存在
                    $value['num']=$value['num']+$request->input('num');

                    $a=1;
                }
            }
        }

        if(!$a){ //未找到相同id
            $data[]=array(
                "id"=>$request->input('id'),//$_GET['id']
                "num"=>$request->input('num'),
                "goodsInfo"=>Goods::where("id",$request->input('id'))->first()
            );
        }

        $request->session()->put('shop', $data);
        return redirect("car");

    }

    //ajax 购物车添加
    public function CarAdd(Request $request){
       // var_dump($request->all());
        //获取修改的id
        $id = $request->input('id');

        //获取session中的商品数据
        $shop= session("shop");

        //遍历数据
        foreach($shop as $key => $value){
            //修改数量
            if($value['id']==$id){
                $shop[$key]['num']=++$shop[$key]['num'];

            }
        }
        //写入session
        $request->session()->put('shop', $shop);

        return 1;

    }

    //ajax 购物车减少
    public function CarJian(Request $request){
        //获取修改的id
        $id = $request->input('id');

        //获取session中的商品数据
        $shop= session("shop");

        //遍历数据
        foreach($shop as $key => $value){
            //修改数量
            if($value['id']==$id){
                $shop[$key]['num']=--$shop[$key]['num'];

            }
        }
        //写入session
        $request->session()->put('shop', $shop);

        return 1;
    }

    //购物车的删除
    public function  CarDel(Request $request){
        //接受id
        $id = $request->input("id");

        //获取购物车中所有的商品数据
        $shop = session('shop');

        //遍历数据
        foreach($shop as $key => $value){
            //判断需要删除的数据
            if($value['id']==$id){

                unset($shop[$key]);
            }
        }
        //写入session
        $request->session()->put('shop',$shop);

        //返回
        return response() -> json(1);

    }

    //结算方法
    public function jiesuan(Request $request){

        //查询当前登陆者的收货地址
        $uid = session('lenovoHomeUserInfo.id');//登录者id
        //dd($uid);
        //查询数据
        $addr = Addr::where("uid",$uid)->get();
        //dd($addr);

        //接收到商品数据
        $idArr = $request->input('goods');
        //dd($idArr);
        //读取session
        $shop= session("shop");
        //dd($shop);

        //声明新数组
        $newArr=array();

        $tot =0;
        //遍历购物车中所有的商品
        if($idArr){
            foreach($idArr as $key => $value){

                foreach($shop as $k => $v){
                    //判断是否用户选择的商品
                    if($v['id']==$value){
                        $v['money']=$v['num']*$v['goodsInfo']->price;//小合计
                        $tot += $v['money'];    //叠加
                        $newArr[] = $v;

                    }
                }
            }
        }

       // dd($newArr);


        //加载结算页面
        return view("home.jiesuan")->with("newShop",$newArr)->with('tot',$tot)->with('addr',$addr);
    }

}
